// pages/shop/shop.js
const httpUrl = getApp().globalData.httpUrl;

const hideLoading = () =>{
  return new Promise((resolve, reject) => {
    wx.hideLoading({
      success: () =>{
        resolve(true); // 成功关闭loading，返回true
      },
      fail: () =>{
        reject(false); // 关闭loading失败，返回false
      }
    });
  });
};
Page({

  /**
   * 页面的初始数据
   */
  data: {
    uid :'',
    shop:{},
    show:false,
    num:1,
    showImage:false,
    image:'',
  },
  show(e){
    this.setData({
      showImage:!this.data.showImage,
      image:e.currentTarget.dataset.item.image
    })
  },

  onClickHide(){
    this.setData({
      showImage:!this.data.showImage
    })
  },

  // 结算订单
  shopping(){
    wx.navigateTo({
      url: '/pages/order/order?num=' + this.data.num + '&shopId=' + this.data.shop.uid,
    })
  },
  onChange(event) {
    this.setData({
      num:event.detail
    })
  },
  onClose(){
    this.setData({
      show:!this.data.show
    })
  },
  onClickButton(){
    console.log("购买");
  },
  onClickIcon(){
    wx.switchTab({
      url: '/pages/cart/cart',
    })
  },
  addCart(){
    wx.request({
      url: httpUrl + '/cart',
      method:'POST',
      header:{
        'Shop_token':wx.getStorageSync('Shop_token')
      },
      data:{
        shopId:this.data.uid
      },
      success(res){
        if (res.data.code == 20011) {
          wx.showToast({
            title: res.data.msg,
            icon:'none',
            duration:500
          })
        }
        else if (res.data.code == 401) {
          console.log(res.data.msg);
          wx.removeStorageSync('Shop_token');
        }else{
          console.log(res.data.msg);
        }
      },
      fail(err){
        console.log(err);
      }
    })
  },
  getShop(){
    var that = this;
    wx.request({
      url: httpUrl + '/shop/'+that.data.uid,
      method:'GET',
      header:{
        'Shop_token':wx.getStorageSync('Shop_token')
      },
      success(res){
        if (res.data.code == 20041) {
          that.setData({
            shop:res.data.data
          })
        }
        else if (res.data.code == 401) {
          console.log(res.data.msg);
          wx.removeStorageSync('Shop_token');
        }else{
          console.log(res.data.msg);
        }
      },
      fail(err){
        console.log(err);
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      uid:options.uid
    },()=>{
      this.getShop();
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getShop();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})